package com.baidu.user.controller;

import java.util.HashMap;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.user.service.AppService;
import com.baidu.user.service.runThread;

@Controller
public class AppController {
			
	@Autowired
	private AppService service;
	
	
	@RequestMapping("App")
	@ResponseBody
	public String App(){
		
		
		String url="http://mm.10086.cn/android/software/qbrj";
		
		try {
			
			
			Parser parser=new Parser(url);
			parser.setEncoding("UTF-8");
			HasAttributeFilter attributeFilter=new HasAttributeFilter("class","last");
			
			
			NodeList nodeList = parser.parse(attributeFilter);
			
			System.out.println();
		
			//总抓取量
			int totalNum=Integer.parseInt(((TagNode)nodeList.elementAt(0)).getAttribute("href").split("=")[1]);
			//每个线程的量
			int threadOneNum=10;
			//计算线程量
			int threadNum=totalNum/threadOneNum+(threadOneNum%totalNum==0?0:1);
			
		for (int i = 0; i < threadNum; i++) {
			
			runThread thread =new runThread(threadOneNum*i+1,threadOneNum*(i+1)>totalNum?totalNum:threadOneNum*(i+1),service);
			thread.start();
		}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new HashMap<String,String>().put("导入数据库", "成功");
		
		
		
	}
}
